import {batchattrValidate,timeFn} from './batchattr-validate'
let formValidate = {
  data(){
    let numberValidator = (rule,value,callback) => {
      if(value===''){
        return callback(new Error(this.$t('m.choose')))
      } else if(!Number.isInteger(value)){
        return callback(new Error(this.$t('m.check.format')))
      }
      callback()
    }
    return {
      ruleForm:{
        warehouseid:[{ required:true, message:this.$t('m.choose')+this.$t('m.page.warehouse'), trigger:'blur'}],
        consignorid:[{ required:true, validator:numberValidator, trigger:'change' }],
        supplierid:[{ required:true, validator:numberValidator, trigger:'change' }],
        currencyid:[{ required:true, validator:numberValidator, trigger:'change' }],
        createstart:[{ required:true, message:this.$t('m.enter')+this.$t('m.otherstore.indate'), trigger:'blur'}]
      }
    }
  }
}
let tableValidate = {
  data(){
    let numberValidator = (rule,value,callback) => {
      if(value===''){
        return callback(new Error(this.$t('m.enter')))
      } else if(value<=0){
        return callback(new Error(this.$t('m.enter')+this.$t('m.check.number0')))
      }
      callback()
    }
    let floatNumberValidator = (rule,value,callback) =>{
      let pattern = /(^[-+]?[1-9]\d*(\.\d{1,2})?$)|(^[-+]?[0]{1}(\.\d{1,2})?$)/ // 小数点后只有2位数字
      if(value===''){
        return callback(new Error(this.$t('m.enter')))
      } else if(isNaN(value)){
        return callback(new Error(this.$t('m.check.format')))
      } else if(value<0){
        return callback(new Error(this.$t('m.enter')+this.$t('m.check.number0')))
      } else if(!pattern.test(value)){
        return callback(new Error(this.$t('m.check.decimal')))
      }
      callback()
    }
    return {
      ruleTable:{
        quantity:[{ validator:numberValidator, trigger:'blur' }],
        unitprice:[{ validator:floatNumberValidator, trigger:'blur' }],
        locationid:[{ required:true, message:this.$t('m.choose')+this.$t('m.page.location'), trigger:'change'}],
        ...batchattrValidate
      }
    }
  },
  methods:{
    ...timeFn
  }
}
export {
  formValidate,
  tableValidate
}